<HTML>

<?php

	/*
		utilities/pic_selector v1.01 - Copyright (c) 2004, Andreas John aka Jack (tR)
		This program and it's moduls are Open Source in terms of General Public Licens (GPL) v2.0

		pic_selector.php 		- picture filename selection system (for html forms)
	*/

	require_once( "classes/csvfile.inc.php" );
	
	require( dirname(__FILE__) . "/../settings/pic_selector.settings.inc.php" );
	
	$file_index = array();
	
// case 1 - auto index mode:
// $pic_list[###]["index"] = "auto";

	if ( $index == "auto" )
	{
		$my_dir = dir($base_dir ); $i = 0;
		while( $entry = $my_dir->read()) 
		{	
			if (!is_dir($entry) && eregi( $legal_exts, $entry) ) 
			{ 
				$file_index[$i] = array();
				$file_index[$i]["filename"]     = $entry;
				$file_index[$i]["return_value"] = $entry;
				$file_index[$i]["comment"]      = $entry;			     
				$i++;
			}
		}
	}
	else
// case 2 - using index file:
// $pic_list[###]["index"] = "pic_index.csv";
	{
		$index_file          = new csvfile();
		$index_file->name    = $base_dir."/".$index;
		$index_file->init();
		
		$entries = $index_file->entries();
		if ($entries >0)
		{
				$red_data = array(); $i=0;
				$index_file->get_entry( 0, $red_data );

				while ($i < $entries )
				{
					$file_index[$i] = array();
					$file_index[$i]["filename"]     = $red_data["filename"];
					$file_index[$i]["return_value"] = $red_data["return_value"];
					$file_index[$i]["comment"]      = $red_data["comment"];

					$index_file->get_next_entry( $red_data );
					$i++;
				}			
		}
		else { echo "index file is empty!"; exit; }
	}
		
	echo <<<JSCRIPT
<SCRIPT language="JavaScript">
	this.focus();

	function return_val( val )
	{
		opener.document.$return_field.value = "image::"+val;
		window.close();
	}
</SCRIPT>
JSCRIPT;

	echo "\n<table border=\"0\" align=\"center\" cellpadding=\"5\">\n";
	
	$keys = array_keys( $file_index );
	
	$counter = 0;
	foreach ( $keys as $key )
	{
			if ($counter == 0) echo "<tr>\n";
			
			echo "  <td><a href=\"javascript:return_val('".$file_index[$key]["return_value"]."')\"><img border=\"0\" src=\"".$base_dir."/".$file_index[$key]["filename"]."\" alt=\"".$file_index[$key]["comment"]."\"></a></td>\n"; 
			$counter++;
			
			if ($counter >= $xwidth) { $counter = 0; echo "</tr>\n"; }
	}
	
	if ($counter != 0) { echo "</tr>\n"; }
	echo "</table>\n";	

?>
</HTML>